package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.CollectionBehaviorDto;
import com.heima.behavior.pojos.ApCollectionBehavior;
import com.heima.behavior.service.ApCollectService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApCollectServiceImpl implements ApCollectService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /*
    * 收藏行为
    * */
    @Override
    public ResponseResult collect(CollectionBehaviorDto dto) {

        //1. 校验参数
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.获取登录人信息
        ApUser apUser = AppThreadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //2.判断用户是否已经收藏，查询redis
        //2.1 获取收藏缓存hash类型的key
        String behavior_hash_key="USER_COLLECTION_"+dto.getArticleId();
        //2.3判断是已经收藏，查询关注缓存中的小key是否包含点赞人id
        Boolean hasKeyFlag = redisTemplate.opsForHash().hasKey(behavior_hash_key, apUser.getId()+"");


        //3.用户未收藏
        if(!hasKeyFlag && dto.getOperation()==0){//要执行点赞行为操作
            //保存数据到redis中
            redisTemplate.opsForHash().put(behavior_hash_key,apUser.getId()+"","1");
            //保存数据到mongo中
            ApCollectionBehavior apLikesBehavior = new ApCollectionBehavior();
            apLikesBehavior.setType(dto.getType());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setUserId(apUser.getId().longValue());
            apLikesBehavior.setCollectionTime(new Date());
            mongoTemplate.insert(apLikesBehavior);
            return ResponseResult.okResult(3009,"  ");
        }
        //4.用户已 收藏
        if(hasKeyFlag && dto.getOperation()==0){//表示已经点赞了，无须任何操作
            return ResponseResult.okResult(3010,"  ");
        }
        if(hasKeyFlag && dto.getOperation()==1){//表示要执行取消收藏操作
            //3.1 则删除缓存数据
            redisTemplate.opsForHash().delete(behavior_hash_key,apUser.getId()+"");
            //3.2 删除mongodb中的数据
            Query query= Query.query(Criteria.where("userId").is(apUser.getId())
                    .and("articleId").is(dto.getArticleId()));
            mongoTemplate.remove(query,ApCollectionBehavior.class);
            return ResponseResult.okResult(3011,"   ");
        }

        return null;
    }
}